<?php
include 'mysql.php';

if(isset($_SERVER['REQUEST_METHOD'])&&strtolower($_SERVER['REQUEST_METHOD'])=='post')
{

    if($_FILES['photo'] )
    {
        $filename = $_FILES['photo']['type'];
        $newname = './photo/'.time().'.jpg';
        if(in_array(strtolower($filename),array('image/jpeg','image/png')))
        {
            if(!move_uploaded_file($_FILES['photo']['tmp_name'] ,$newname) )
            {
                die ('图片上传失败');
            }
            else
            {
                $query = "INSERT INTO  `photo` (`id`, `url`) VALUES (NULL, '$newname')";
                $result=$mysqli->query($query);
                if(!$result)
                {
                    echo  "<script>alert('添加失败')</script>";
                }

            }
        }

    }


}


$list = array() ;
$result = $mysqli->query("select * from photo");
while($row =$result->fetch_array() ){
    $list[] = $row;
}

?>

    <!doctype html>
    <html>
<head>
    <title>后台管理</title>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <link href="css/admin_full.css" rel="stylesheet" />
    <script src="js/jquery.js"></script>
    <script type="text/javascript">
		$(function(){

            $('form[name="form_submit"]').submit(function(){

                var photo = $('#photo').val();

                if($('#photo').val() == ""){
                    $('#photo').focus();
                    return false ;
                }

            });
        })

		</script>
</head>
<body>

 <div class="wrap J_check_wrap">
    <div class="h_a">功能说明</div>
    <div class="prompt_text">
       发布照片
    </div>


    <div class="h_a">活动图片</div>
    <div class="table_list" id="DataBox">
            <table width="100%">
                <thead>
                <tr>
                    <td>照片ID</td>
                    <td>照片</td>
                    <td>操作</td>
                  </tr>
                </thead>
                <?php if(!empty($list)&&isset($list)):?>
                    <?php foreach($list as $val):?>
                        <tr>
                        <td> <?php echo $val['id'];?></td>
                         <td><img width="120" height="80" src="<?php echo @$val['url'];?>"></td>
                            <td> <a href="action.php?action=delete_pic&id=<?php echo $val['id'];?>">删除</a></td>
                        </tr>
                    <?php endforeach;?>
                <?php endif;?>

             </table>
    </div>


    <div class="h_a">发布活动</div>
    <div class="search_type cc mb10">
        <form action=" " method="post" name="form_submit" enctype="multipart/form-data">
             <div class="ul_wrap">
                <label>添加照片：</label>
                <input type="file" class="input length_3" name="photo" id="photo"  >
             </div>
             <div class="ul_wrap"> <button class="btn" type="submit">确定</button> </div>
        </form>
    </div>
</div>
</body>
</html>

<?php $mysqli->close(); ?>